package org.example.web;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.dto.ProductRequestDto;
import org.example.dto.ProductResponseDto;
import org.example.dto.ResultDto;
import org.example.service.ProductService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Slf4j
public class ProductController {                   //产品服务
    private final ProductService productService;
    @PostMapping("/api/product")
    @ResponseStatus(HttpStatus.CREATED)
    public void create(@RequestBody ProductRequestDto productRequest){
        productService.create(productRequest);
        log.info("产品{}创建成功",productRequest.getName());
    }

    @GetMapping("/api/product")
    @ResponseStatus(HttpStatus.OK)
    public ResultDto<List<ProductResponseDto>> findAll(){
       List<ProductResponseDto> list =  productService.findAll();
        return ResultDto.<List<ProductResponseDto>>builder()
                .code("ok")
                .data(list)
                .build();
    }
}
